/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package vn.ac.c0910g.assignment.DAO;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import vn.ac.c0910g.assignment.Entity.Users;
import vn.ac.c0910g.assignment.Entity.UsersFacade;

/**
 *
 * @author BachLe
 */
public class UsersDAO {
    UsersFacade usersFacade = lookupUsersFacadeBean();

    public UsersDAO(){

    }
    private UsersFacade lookupUsersFacadeBean() {
        try {
            Context c = new InitialContext();
            return (UsersFacade) c.lookup("java:global/Alluring_Decors/Alluring_Decors-ejb/UsersFacade!vn.ac.c0910g.assignment.Entity.UsersFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    public List<Users> getAllUsers(){
        return this.usersFacade.findAll();
    }

    public void addUsers(Users u){
        usersFacade.create(u);
    }

    public void updateUsers(Users u){
        usersFacade.edit(u);
    }

    public void deleteUsers(Users u){
        usersFacade.remove(u);
    }

    public Users findUsersById(String Id){
        return usersFacade.find(Id);
    }

    public boolean checkLogin(String name, String pass){
        boolean b= false;
        if(usersFacade.getUserByUsername(name, pass) != null){
            b = true;
        }else{
            b = false;
        }
        return b;
    }

    public boolean addnewUser(Users u){
        boolean b= false;
        if(usersFacade.insertUser(u) != null){
            b = true;
        }else{
            b = false;
        }
        return b;
    }
}
